package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.NurseBean;
import com.hxkcloud.patient.config.UrlConfig;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.viewholders.DiscoverNurseViewholder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-07-14.
 * Description:患者发现页的护士列表
 */
public class DiscoverNursesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NurseBean> datas;
    private Context mContext;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader;
    private onNursesClickListener listener;
    private int ndType = 0;//0护士 1医生

    public DiscoverNursesAdapter(Context mContext, List<NurseBean> datas, int type, onNursesClickListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.ndType = type;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
        this.listener = listener;
    }

    public void setData(List<NurseBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_discovernurses, null);
        DiscoverNurseViewholder holder = new DiscoverNurseViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DiscoverNurseViewholder mHolder = (DiscoverNurseViewholder) holder;
        NurseBean bean = datas.get(position);

        String img = bean.getImg();
        if (!img.startsWith("http")) {
            img = UrlConfig.image_host + img;
        }
        if (ndType == 1) {
            if (bean.getSex().equals("0")) {
                imageLoader.displayImage(img, mHolder.civ_head, ImageOptionUtils.getInstance().getDoctorHeadMaleOptions());
            } else {
                imageLoader.displayImage(img, mHolder.civ_head, ImageOptionUtils.getInstance().getDoctorHeadFemaleOptions());
            }
        } else {
            imageLoader.displayImage(img, mHolder.civ_head, ImageOptionUtils.getInstance().getNurseHeadOptions());
        }
        mHolder.civ_head.setBorderWidth(2);
        mHolder.civ_head.setBorderColor(ContextCompat.getColor(mContext, R.color.grey_text2));
        if (ndType == 0) {
            if (bean.getIsVip().equals("0")) {
                mHolder.iv_vip.setVisibility(View.VISIBLE);
            } else {
                mHolder.iv_vip.setVisibility(View.INVISIBLE);
            }
        } else {
            mHolder.iv_vip.setVisibility(View.INVISIBLE);
        }

        if (ndType == 0) {
            if (bean.getIsBinding().equals("0")) {
                mHolder.tv_binding.setVisibility(View.VISIBLE);
                mHolder.view_alpha.setVisibility(View.GONE);
                mHolder.tv_talk.setVisibility(View.VISIBLE);

                ((LinearLayout.LayoutParams) mHolder.ll_talk.getLayoutParams()).height = LinearLayout.LayoutParams.WRAP_CONTENT;
            } else {
                mHolder.tv_binding.setVisibility(View.INVISIBLE);
                mHolder.view_alpha.setVisibility(View.VISIBLE);
                mHolder.tv_talk.setVisibility(View.GONE);

                ((LinearLayout.LayoutParams) mHolder.ll_talk.getLayoutParams()).height = LinearLayout.LayoutParams.MATCH_PARENT;
            }
        } else {
            mHolder.tv_binding.setVisibility(View.INVISIBLE);
            mHolder.view_alpha.setVisibility(View.GONE);
            mHolder.tv_talk.setVisibility(View.GONE);

            ((LinearLayout.LayoutParams) mHolder.ll_talk.getLayoutParams()).height = LinearLayout.LayoutParams.MATCH_PARENT;
        }
        mHolder.tv_talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onTalk(position);
            }
        });
        mHolder.tv_name.setText(bean.getName());
        mHolder.tv_title.setText(bean.getTitle());

//        mHolder.tv_year.setText("从业" + bean.getYear() + "年");
        if (TextUtils.isEmpty(bean.getYear())) {
//            mHolder.tv_year.setVisibility(View.GONE);
            mHolder.tv_year.setText("从业<1年");
        } else {
            mHolder.tv_year.setVisibility(View.VISIBLE);
            long dateBegin = (new Date(Long.valueOf(bean.getYear()))).getTime();
            long dateNow = (new Date()).getTime();
            long year = (dateNow - dateBegin) / 1000 / 60 / 60 / 24 / 365;
            if (year >= 1) {
                mHolder.tv_year.setText("从业" + (int) year + "年");
            } else {
                mHolder.tv_year.setText("从业<1年");
            }
        }

        mHolder.ll_info.measure(0, 0);
        RelativeLayout.LayoutParams lp_view = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mHolder.ll_info.getMeasuredHeight());
        mHolder.view_alpha.setLayoutParams(lp_view);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface onNursesClickListener {
        void onTalk(int position);
    }
}
